﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using DACU.Tools;

namespace DACU.VkontakteApi.Base
{
	public class VKAudio
	{
		public string Id { get; set; }
		public string OwnerId { get; set; }
		public string Artist { get; set; }
		public string Title { get; set; }
		public string Duration { get; set; }
		public string LyricsId { get; set; }
		public string Url { get; set; }
		public TimeSpan DurationTime
		{
			get
			{
				return String.IsNullOrWhiteSpace(Duration) ? TimeSpan.Zero : TimeSpan.FromSeconds(Duration.ToDouble());
			}
		}
		public string GetTimeStamp { get; set; }
		public bool IsEmpty
		{
			get
			{
				bool isE = true;
				if (!String.IsNullOrWhiteSpace(Id))
					if (!String.IsNullOrWhiteSpace(Artist))
						if (!String.IsNullOrWhiteSpace(Title))
							if (!String.IsNullOrWhiteSpace(Url))
								isE = false;
				return isE;
			}
		}

		public static VKAudio ParseXml(XElement response)
		{
#region
			/*
			 * aid,
			 * owner_id,
			 * artist,
			 * title,
			 * duration,
			 * url
			 * lyrics_id
			 */
#endregion
			if (response == null || response.IsEmpty) throw new ArgumentNullException("response");
			var audio = new VKAudio
			            	{
			            		Artist = XmlUtils.GetNodeString(response.Element("artist")),
			            		Duration = XmlUtils.GetNodeString(response.Element("duration")),
			            		Id = XmlUtils.GetNodeString(response.Element("aid")),
			            		LyricsId = XmlUtils.GetNodeString(response.Element("lyrics_id")),
			            		OwnerId = XmlUtils.GetNodeString(response.Element("owner_id")),
			            		Title = XmlUtils.GetNodeString(response.Element("title")),
			            		Url = XmlUtils.GetNodeString(response.Element("url")),
			            		GetTimeStamp = DateTime.UtcNow.ToString()
			            	};
			return audio;
		}

		public override string ToString()
		{
			return Id + " | " + Artist + " | " + Title;
		}
	}
}
